package com.mc.dao.Impl;

import com.mc.bean.Apply;
import com.mc.dao.ApplyDao;
import com.mc.dao.Impl.BaseDao;

import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @author zh
 * 报名实体类数据访问接口实现
 */
public class ApplyDaoImpl extends BaseDao implements ApplyDao {

    @Override
    public List<Apply> findAll() {
        //创建list集合
        List<Apply> list=new ArrayList<>();
        try {
            //1.注册驱动
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="select * from apply";
            super.getPst(sql);
            super.connection();
            rs=pstmt.executeQuery();
            while (rs.next()){
                //创建对象
                Apply apply=new Apply(rs.getInt(1),rs.getInt(2),rs.getInt(3));
            //将对象放入集合中
                list.add(apply);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    @Override
    public Apply findByCode(int matchcode) {
        Apply apply=null;
        try {
            //1.数据库连接
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="select * from apply where matchcode=?";
            super.getPst(sql);
            super.pstmt.setInt(1,matchcode);
            //执行预编译指令得到结果
           getRs();
           while (rs.next()){
               apply=new Apply(rs.getInt(1),rs.getInt(2),rs.getInt(3));

           }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return apply;
    }

    @Override
    public List<Apply> findByMid(int mid) {
        List<Apply> list=new ArrayList<>();
        try {
            //1.数据库连接
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="select * from apply where mid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,mid);
            //执行预编译指令得到结果
            getRs();
            while (rs.next()){
              Apply  apply=new Apply(rs.getInt(1),rs.getInt(2),rs.getInt(3));
                list.add(apply);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    @Override
    public List<Apply> findByTeamId(int teamid) {

        List<Apply> list=new ArrayList<>();
        try {
            //1.数据库连接
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="select * from apply where teamid=?";
            super.getPst(sql);
            super.pstmt.setInt(1,teamid);
            //执行预编译指令得到结果
            getRs();
            while (rs.next()){
                Apply  apply=new Apply(rs.getInt(1),rs.getInt(2),rs.getInt(3));
                list.add(apply);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            //关闭
            close();
        }
        return list;
    }

    @Override
    public int add(Apply apply) {
        int i=0;
        try {
            //1.数据库连接
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="insert into Apply(mid,teamid)values(?,?)";
            super.getPst(sql);
            pstmt.setInt(1,apply.getMid());
            pstmt.setInt(2,apply.getTeamid());
            //执行预编译指令，得到结果
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }

    @Override
    public int delete(int matchcode) {

        int i=0;
        try {
            //1.数据库连接
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="delete from apply where matchcode=?";
            super.getPst(sql);
            pstmt.setInt(1,matchcode);
            //执行预编译指令，得到结果
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }


    @Override
    public int update(Apply apply) {
        int i=0;
        try {
            //1.数据库连接
            super.regist();
            //2.数据库连接
            super.connection();
            //3.创建sql语句+预编译指令+设置指定参数
            String sql="update apply set mid=?,teamid=? where matchcode=?";
            super.getPst(sql);

            pstmt.setInt(1,apply.getMid());
            pstmt.setInt(2,apply.getTeamid());
            pstmt.setInt(3,apply.getMatchcode());
            //执行预编译指令，得到结果
            i=pstmt.executeUpdate();
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        } finally {
            close();
        }
        return i;
    }
}
